<?php


namespace app\admin\controller;



use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class GradeController extends AdminBaseController
{
    //显示
    public  function index(){

        //连接成绩表
        $data=db('grade')
            //重命名
            ->alias('g')
            //学生表
            ->join( 'student stu','stu.id = g.student_id','LEFT')
            //学校表
            ->join( 'subject sub','sub.id = g.subject_id','LEFT')
            //查询指定字段
            ->field('stu.student_name,sub.subject,g.*')
            //通过id倒序
            ->order('id desc')
            //每页显示3条
            ->paginate(3);
//halt($data);

        //分页
        $page= $data ->render();
        //搜索学校
        $school=db('school')->select();
        //渲染模板
        $this->assign('data',$data);
        $this->assign('page',$page);
        //映射页面
        return $this->fetch();
    }

    //添加
    public function add(){
        //查询所有学生
        $student=db('student')->select();
        //查询所有科目
        $subject =db('subject')->select();
        //渲染模板
        $this->assign('subject',$subject);
        $this->assign('student',$student);
        //映射页面
        return $this->fetch();
    }

    //将添加数据入库
    public function addpost(){
        //接收数据
        $data= input();
        //halt($data);

        //添加到数据库
        $add = db('grade')->insert($data);
        //判断是否成功
        if ($add){
            $this->success('添加成功','grade/index');
        }else{
            $this->error('添加失败','grade/index');
        }
    }

    //修改
    public function edit(){
        //接收id
        $id = input('id');
        //通过id查询该信息
        $grade=db('grade')->get($id);
        $student=db('student')->select();
        $subject=db('subject')->select();
      //halt($grade);
        //渲染模板
        $this->assign('grade',$grade);
        $this->assign('student',$student);
        $this->assign('subject',$subject);
        //映射页面
        return $this->fetch();
    }
    //保存修改信息
    public function editpost(){
        //接收数据
        $data= input();

        //halt($data);
        $edit = db('grade')->update($data);
        if ($edit){
            $this->success('修改成功','grade/index');
        }else{
            $this->error('修改失败','grade/index');
        }
    }

    //删除
    public function delete()
    {
        //查询要删除的id
        $id = $this->request->param('id', 0, 'intval');

        //通过id删除该条信息，并判断是否成功
        if (Db::name('grade')->delete($id) !== false) {

            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    //查询该学校id下的所有班级
    public function getSchool(){
        $schoolId=input('school_id');
        //halt($schoolId);
        $room=db('room')
            ->where('school_id',$schoolId)->select();
       // halt($room);
        if ($room){
            $options=['<option value="">--请选择班级--</option>'];
            foreach ($room as $v){
                $opt="<option value='".$v['id']."'>".$v['room_name']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }


}